package com.open.api.wechat.app.impl;

import com.open.api.wechat.config.properties.WeChatAppProperties;
import com.open.api.wechat.app.request.*;
import com.open.api.wechat.app.request.condition.*;
import com.open.api.wechat.app.response.*;
import com.open.api.wechat.app.WeChatAppService;
import com.open.api.wechat.common.WeChatClient;
import com.open.api.wechat.common.WeChatCommonRequest;
import com.open.api.wechat.common.WeChatCommonResponse;
import com.open.api.wechat.common.request.WeChatAccessTokenRequest;
import com.open.api.wechat.common.response.WeChatAccessTokenResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author : zhaoxuan
 * @date : 2021/6/25 15:08
 * @description : 小程序交互类
 * @version: 1.0
 */
@Data
@AllArgsConstructor
public class WeChatAppServiceImpl implements WeChatAppService {

    private static final String APP_ID = "APPID";

    private static final String APP_SECRET = "APPSECRET";

    private static final String JS_CODE = "JSCODE";

    private static final String OPEN_ID = "OPENID";

    private WeChatAppProperties weChatAppProperties;

    @Override
    public WeChatAccessTokenResponse getAccessToken() {
        WeChatAccessTokenRequest request = new WeChatAccessTokenRequest();
        String url = request.getDefaultUrl()
                .replace(APP_ID, weChatAppProperties.getAppId())
                .replace(APP_SECRET, weChatAppProperties.getAppSecret());
        request.setUrl(url);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatCode2SessionResponse getCode2Session(String jsCode) {
        WeChatCode2SessionRequest request = new WeChatCode2SessionRequest();
        String url = request.getDefaultUrl()
                .replace(APP_ID, weChatAppProperties.getAppId())
                .replace(APP_SECRET, weChatAppProperties.getAppSecret())
                .replace(JS_CODE, jsCode);
        request.setUrl(url);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatPaidUnionIdResponse getPaidUnionId(WeChatPaidUnionIdCondition condition, String accessToken) {
        WeChatPaidUnionIdRequest request = new WeChatPaidUnionIdRequest();
        String url = request.getDefaultUrl()
                .replace(WeChatCommonRequest.ACCESS_TOKEN, accessToken)
                .replace(OPEN_ID, condition.getOpenId());
        if (StringUtils.isNotBlank(condition.getTransactionId())) {
            url += ("&transaction_id=" + condition.getTransactionId());
        }
        if (StringUtils.isNotBlank(condition.getMchId())) {
            url += ("&mch_id=" + condition.getMchId());
        }
        if (StringUtils.isNotBlank(condition.getOutTradeNo())) {
            url += ("&out_trade_no=" + condition.getOutTradeNo());
        }
        request.setUrl(url);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatRetainResponse getDailyRetain(WeChatRetainCondition condition, String accessToken) {
        WeChatRetainRequest request = new WeChatRetainRequest();
        request.choiceDailyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }

    @Override
    public WeChatRetainResponse getMonthlyRetain(WeChatRetainCondition condition, String accessToken) {
        WeChatRetainRequest request = new WeChatRetainRequest();
        request.choiceMonthlyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatRetainResponse getWeeklyRetain(WeChatRetainCondition condition, String accessToken) {
        WeChatRetainRequest request = new WeChatRetainRequest();
        request.choiceWeeklyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatDailySummaryResponse getDailySummary(WeChatDailySummaryCondition condition, String accessToken) {
        WeChatDailySummaryRequest request = new WeChatDailySummaryRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }

    @Override
    public WeChatVisitTrendResponse getDailyVisitTrend(WeChatVisitTrendCondition condition, String accessToken) {
        WeChatVisitTrendRequest request = new WeChatVisitTrendRequest();
        request.choiceDailyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatVisitTrendResponse getMonthlyVisitTrend(WeChatVisitTrendCondition condition, String accessToken) {
        WeChatVisitTrendRequest request = new WeChatVisitTrendRequest();
        request.choiceMonthlyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatVisitTrendResponse getWeeklyVisitTrend(WeChatVisitTrendCondition condition, String accessToken) {
        WeChatVisitTrendRequest request = new WeChatVisitTrendRequest();
        request.choiceWeeklyUrl();
        request.setCondition(condition);
        return WeChatClient.execute(request);
    }

    @Override
    public byte[] createQRCode(WeChatCreateQRCodeCondition condition, String accessToken) {
        WeChatCreateQRCodeRequest request = new WeChatCreateQRCodeRequest();
        request.setCondition(condition);
        try(Response response = WeChatClient.executeFileBytes(request, accessToken)) {
            return response.body().bytes();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    public byte[] getQRCode(WeChatGetQRCodeCondition condition, String accessToken) {
        WeChatGetQRCodeRequest request = new WeChatGetQRCodeRequest();
        request.setCondition(condition);
        try(Response response = WeChatClient.executeFileBytes(request, accessToken)) {
            return response.body().bytes();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    public byte[] getUnlimitedQRCode(WeChatUnlimitQRCodeCondition condition, String accessToken) {
        WeChatUnlimitQRCodeRequest request = new WeChatUnlimitQRCodeRequest();
        request.setCondition(condition);
        try(Response response = WeChatClient.executeFileBytes(request, accessToken)) {
            return response.body().bytes();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    public WeChatUrlSchemeResponse getUrlScheme(WeChatUrlSchemeCondition condition, String accessToken) {
        WeChatUrlSchemeRequest request = new WeChatUrlSchemeRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }

    @Override
    public WeChatUrLinkResponse getUrlLink(WeChatUrLinkCondition condition, String accessToken) {
        WeChatUrLinkRequest request = new WeChatUrLinkRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }

    @Override
    public WeChatCommonResponse sendServiceMessage(WeChatServiceMessageCondition condition, String accessToken) {
        WeChatServiceMessageRequest request = new WeChatServiceMessageRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }

    @Override
    public WeChatCommonResponse sendSubscribeMessage(WeChatSubscribeMessageCondition condition, String accessToken) {
        WeChatSubscribeMessageRequest request = new WeChatSubscribeMessageRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }
}
